package org.idea.qiyu.framework.rpc.dubbo.router.starter.filter;

import com.alibaba.fastjson.JSON;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.idea.qiyu.common.constants.RequestConstants;
import org.idea.qiyu.common.context.QiyuCommonRequest;
import org.idea.qiyu.common.context.QiyuRequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.idea.qiyu.common.constants.RequestConstants.QIYU_BASE_REQUEST;

/**
 * @Author linhao
 * @Date created in 4:51 下午 2021/7/30
 */
@Activate(group = {CommonConstants.PROVIDER}, order = -100000)
public class ProviderContextFilter implements Filter {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProviderContextFilter.class);

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        try {
            String reqJson = invocation.getAttachment(String.valueOf(RequestConstants.QIYU_BASE_REQUEST));
            if (!StringUtils.isEmpty(reqJson)) {
                QiyuCommonRequest qiyuCommonRequest = JSON.parseObject(reqJson, QiyuCommonRequest.class);
                invocation.setAttachment(String.valueOf(QIYU_BASE_REQUEST), qiyuCommonRequest.getTraceIdList());
                qiyuCommonRequest.getTraceIdList().add(UUID.randomUUID().toString());
                String headTraceId = qiyuCommonRequest.getTraceIdList().get(0);
                if (!StringUtils.isEmpty(headTraceId)) {
                    qiyuCommonRequest.setHeadTraceId(headTraceId);
                }
                qiyuCommonRequest.setTimestamp(System.currentTimeMillis());
                QiyuRequestContext.put(RequestConstants.QIYU_BASE_REQUEST, qiyuCommonRequest);
                String qiyuCommonRequestJson = JSON.toJSONString(qiyuCommonRequest);

                //日志上报功能
                try {
                    org.idea.qiyu.common.dto.AgentCollectorDataDTO agentCollectorDataDTO = new org.idea.qiyu.common.dto.AgentCollectorDataDTO();
                    agentCollectorDataDTO.setReportType((short) 1);
                    agentCollectorDataDTO.setHeadTraceId(headTraceId);
                    //todo 暂时这么做
                    Map<String, List<String>> itemMap = new HashMap<>();
                    itemMap.put(headTraceId,qiyuCommonRequest.getTraceIdList());
                    agentCollectorDataDTO.getTraceIdMap().add(itemMap);

                    agentCollectorDataDTO.setJson(qiyuCommonRequestJson);
                    agentCollectorDataDTO.setTimestamp(System.currentTimeMillis());
                    org.idea.qiyu.common.utils.ApmAgentUtil.doReport(agentCollectorDataDTO);
                    System.out.println("[provider]日志上报,agentCollectorDataDTO is " + agentCollectorDataDTO);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            LOGGER.error("ConsumerContextFilter e is ", e);
        }
        return invoker.invoke(invocation);
    }
}
